package controllers;

import java.awt.event.ActionEvent;
import java.util.Vector;

import javax.swing.JOptionPane;

import models.ProcessShipmentModel;

import dialogs.InsertTupleDialog;
import dialogs.ProcessTransactionDialog;

import system.View;

public class ProcessShipmentController extends AbstractController {

	private ProcessShipmentModel process_shipment = new ProcessShipmentModel();
	private String supName = "";
	private String sname = "";
	private Vector<Vector<String>> full_shipment = new Vector<Vector<String>>();

	public ProcessShipmentController(View v) {
		super(v);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("Process Shipment"))
			showDialogWindow(new InsertTupleDialog(view, "Process Shipment",
					"Shipment Info", new String[] { "Supplier Name",
							"Store Name" }, this));
	}

	public int validateEntries(Vector<String> s) {
		if (s.size() != 3)
			return VALIDATIONERROR;

		int upc;
		int quantity;
		int price;

		try {
			if (s.get(0).trim().length() != 0)
				upc = Integer.valueOf(s.get(0).trim());
			else return VALIDATIONERROR;
			if (s.get(1).trim().length() != 0)
				quantity = Integer.valueOf(s.get(1).trim());
			else return VALIDATIONERROR;
			if (s.get(2).trim().length() != 0)
				price = Integer.valueOf(s.get(2).trim());
			else return VALIDATIONERROR; 

			if (quantity < 1 || price < 1)
				return VALIDATIONERROR;
	
			for (Vector<String> single_item : full_shipment)
				if (Integer.valueOf(single_item.get(0)) == upc)
					return VALIDATIONERROR;
	
			if (!process_shipment.validateUPC(upc))
				return OPERATIONFAILED;
		} catch (NumberFormatException ex) {
			return VALIDATIONERROR;
		}
		full_shipment.add(s);
		return OPERATIONSUCCESS;
	}

	@Override
	public int validateInsert(Vector<String> s) {

		String sname, supName;
		if (this.sname.equals("") || this.supName.equals("")) {
			if (s.size() != 2)
				return VALIDATIONERROR;
			
			if ((supName = s.get(0).trim()).length() == 0)
				return VALIDATIONERROR;
			if ((sname = s.get(1).trim()).length() == 0)
				return VALIDATIONERROR;

			if (process_shipment.validateShipmentInfo(supName, sname))
			{
				this.sname = sname;
				this.supName = supName;
				showDialogWindow(new ProcessTransactionDialog(view,
						"Process Shipment", "Item Info", new String[] {
								"Item UPC: ", "Quantity", "Price: " }, this));
				return OPERATIONSUCCESS;
			}
			else {
				rollBack();
				JOptionPane.showMessageDialog(view, "Supplier name or store name doesn't exist.", "Error", JOptionPane.ERROR_MESSAGE);
				return operationFailure();
			}
		} else {
			int retCode = validateEntries(s);
			
			if (retCode == OPERATIONSUCCESS) {
				int upc;
				int quantity;
				int price;
				
				for (Vector<String> single_item : full_shipment)
				{
					upc = Integer.valueOf(single_item.get(0).trim());
					quantity = Integer.valueOf(single_item.get(1).trim());
					price  = Integer.valueOf(single_item.get(2).trim());
					if (!process_shipment.processShippedItem(upc, quantity, price))
						return operationFailure();
				}
				
				if (process_shipment.commit()) {
					rollBack();
					return operationSuccess();
				}
				else return operationFailure();
			}
			else return retCode;
		}
	}

	public void rollBack() {
		sname = "";
		supName = "";
		full_shipment.clear();
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub

	}

	@Override
	public int validateDelete(Vector<String> s) {
		// TODO Auto-generated method stub
		return 0;
	}
}
